import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { couldStartTrivia } from 'typescript';

interface RequestParams {
  [key: string]: any;
}


export interface ResponseData {
  code: number;
  message: string;
  data: string;
}

class HttpClient {
  private instance: AxiosInstance;
  private readonly baseURL: string;

  constructor(baseURL: string) {
    this.baseURL = baseURL;
    this.instance = axios.create({
      baseURL: this.baseURL,
      timeout: 10000,
      headers: {
        'Content-Type': 'application/json',
      },
    });
  }

  public async get(url: string, params?: RequestParams, config?: AxiosRequestConfig): Promise<ResponseData> {
    const response = await this.instance.get<ResponseData>(url, {
      params,
      ...config,
    });

    return this.handleResponse(response);
  }


  public async post(url: string, data?: RequestParams, config?: AxiosRequestConfig): Promise<ResponseData> {
    const response = await this.instance.post<ResponseData>(url, data, config);
    return this.handleResponse(response);
  }

  private handleResponse(response: AxiosResponse<ResponseData>): ResponseData {
    const { data } = response;
    return data;
  }
}


const http = new HttpClient('http://127.0.0.1:8080/');

export default http;